<?php
/*
客户管理
*/
//define('Acache_no','1');
class Con_sms extends Acan{
	public $defaultAct='my';
	public $Conread = '短信订阅';
	public $Actread = array('list'=>'所有订阅','my'=>'订阅客户','info'=>'短信订阅信息');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_crm_sms);
		//print_r($this->model);
		$this->_S = Afun_session();
	}
	//--所有客户
	function Act_list(){
		$this->atitle = '所有订阅';
		return $this->Act_my();
	}
	//--自己客户
	function Act_my(){
		if(empty($this->atitle)){$this->atitle = '订阅客户';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$get = $_GET;
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr['username'] = '业务员';
		$columnrr['realname'] = '客户名称';
		$columnrr = array_merge($columnrr,$this->model->comment);//初始栏目
		unset($columnrr['uid'],$columnrr['cid'],$columnrr['sid']);
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		//分页函数
		if($this->Aact=='list' && $this->uid==$uid){
			$con->where = "1".$where;
			if($this->_S['group']>1 && $this->_S['admin']==1){
				$adminjoin = " JOIN `".Acan_table_user_group."` g ON a.uid=g.uid";
				$con->where = "g.`group`=".$this->_S['group']." and g.appid=".Acan_app_id.$where;
			}
		}else if($this->Aact=='list' && $this->uid!=$uid){
			$con->where = "a.uid=$uid".$where;
		}else{
			$con->where = "a.uid=$this->uid".$where;//只能查看自己的客户
		}
		$con->select = "a.*,b.username,c.realname";
		$con->join = "JOIN `".Acan_table_user."` b ON a.uid=b.uid JOIN `".Acan_table_crm_customer."` c on a.cid=c.cid".$adminjoin;
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = "start_date";
		$page->order = "DESC";
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl($this->Amod.'/my');
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$con->select = "a.*,c.realname,c.phone";
		$con->join = "LEFT JOIN `".Acan_table_crm_customer."` c ON a.cid=c.cid";
		$con->where = "a.sid=".$this->id;
		$info = $this->model->ofind($con);
		Afun_json($info);
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一条记录
				$savedata['uid'] = $this->uid;
				$savedata['start_date'] = Afun_get_time();
				$savedata['end_date'] = Afun_time_date(Afun_time_ex(strtotime($savedata['start_date']),1,'m'));
				$savedata['status'] = 1;
				$msg = $this->model->save($savedata);
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_json($msg);
	}
	//--所有客户
	function Act_send(){
		$this->atitle = '发送短信';
		if($_POST){
			$post = $_POST;
			if($post['type']==3){
				$phone = explode("\n",$post['phone']);
			}else{
				$con->select = 'c.phone';
				echo $con->where = 'status=1 and end_date > "'.Afun_get_time().'" and start_date < "'.Afun_get_time().'" and type='.$post['type'];
				$con->join = 'JOIN '.Acan_table_crm_customer.' c ON a.cid=c.cid';
				$rs = $this->model->findAll($con);
				foreach ($rs as $k => $v) {
					$phone[] = $v['phone'];
				}
			}
			if($phone){
				$i=0;
				$msmslog = Afun_db()->model(Acan_table_crm_sms_log);
				$save->note = $post['note'];
				$save->date = Afun_get_time();
				$save->sendtime = $post['sendtime'];
				foreach ($phone as $k => $v) {
					$v = trim($v);
					if(strlen($v)==11){
						$save->phone = $v;
						$msmslog->save($save);
						$i++;
					}
				}
				Afun_refresh('成功发送'.$i.'条','referrer');return;
			}else{
				Afun_alert('号码为空');
			}
		}
		include $this->Atpl();
	}
	//--所有客户
	function Act_autosend(){
		$this->atitle = '自动发送';
		$msmslog = Afun_db()->model(Acan_table_crm_sms_log);
		$con->select = 'a.id,a.phone,a.note';
		$con->where = 'status=0 and (sendtime < "'.Afun_get_time().'" or sendtime is null)';
		$con->limit = '1';
		$obj = $msmslog->ofindAll($con);
		if($obj){
			foreach ($obj as $k => $v) {
				$data[$v->id]['phone'] = $v->phone;
				$data[$v->id]['note'] = $v->note;
			}
			$rs = Afun_curl('post',"http://abans.sinaapp.com/api/sms.php?",array('data'=>$data,'sign'=>'fsd854j909kl349'));
			$rs = json_decode($rs['data']);	
			print_r($rs).'<hr>';
			if(is_object($rs) || is_array($rs))
				foreach ($rs as $id => $v) {
					$up->status = 1;
					if($v->ApiBusError){
						$up->status = 4;
						$up->error = Ajson_encode($v->ApiBusError);
					}else{
						$up->error = Ajson_encode($v->status);
					}
					$msmslog->updatePk($up,$id);
				}
		}
		print_r($obj);
		echo '自动发送'.Afun_get_time();
		Afun_refresh('','',1000);
	}
}

?>